/*
 * Group:PrePool K9-357 
 * 8/2010
 */
package control;

import java.io.IOException;

import javax.portlet.*;

import org.apache.log4j.Logger;

import blo.ManagerindictBLO;

import form.BaseForm;
import form.ManageHome;

/**
 * Control class for S03_Login.jsp 
 * Methods is called in method
 * processAction(ActionRequest request, ActionResponse response) of
 * OnlineDictPortlet class
 */
public class Login extends BaseControl {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger LOG = Logger.getLogger("control.Login");
	/**
	 * Perform when click Login button
	 * 
	 * @param request
	 * @param response
	 * @return no return here
	 * @throws PortletException
	 * @throws IOException
	 */
	public ManageHome procLogin(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
//		LOG.debug("procLogin.START");
		String user = request.getParameter("loginUsername");
		String pass = request.getParameter("loginPassword");
		LOG.info(user);
		// check username and password
		blo.UsersBLO loginUser = new blo.UsersBLO();
		blo.ManagerindictBLO manageBLO = new ManagerindictBLO();
		int role = loginUser.login(user, pass);
		ManageHome result = new ManageHome();

		result.setApproverManager(manageBLO.getApproverManagement(user));
		result.setReviewerManager(manageBLO.getReviewerManagement(user));
		result.setLoginUsername(user);
//		result.setLoginPassword(pass);
		result.setRole(role);
		return result;
	}

	/**
	 * Perform when click Back button
	 * 
	 * @param request
	 * @param response
	 * @return null
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procBack(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
//		LOG.debug("procBack.START");
//		LOG.debug("procBack.START");
		return null;
	}

	/**
	 * Perform when click LogoutButton
	 * 
	 * @param request
	 * @param response
	 * @return null
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogout(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
		request.getPortletSession().setAttribute("username", "Guest",
				PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().setAttribute("userrole", -1,
				PortletSession.APPLICATION_SCOPE);
		return null;
	}

}
